package com.efast.cafe.portal.controller.base;


import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.enums.ResultStatus;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.pager.Pagination;
import com.efast.cafe.portal.bean.base.PortalParameterQueryBean;
import com.efast.cafe.portal.entity.base.BaseCompanyInfo;
import com.efast.cafe.portal.entity.base.PortalBaseParameter;
import com.efast.cafe.portal.entity.common.ModuleInfo;
import com.efast.cafe.portal.entity.company.PortalCompanyUser;
import com.efast.cafe.portal.service.base.IBaseCompanyInfoService;
import com.efast.cafe.portal.service.base.IPortalBaseParameterService;
import com.efast.cafe.portal.service.common.IResourceService;
import com.efast.cafe.portal.service.company.IPortalCompanyUserService;
/**
 * ****************************************************************************
 * @File name   :      PortalBaseParameterController.java
 *
 * @Author      :      qubibo
 *
 * @Date        :      2016-6-15
 *
 * @Copyright Notice: 
 * Copyright (c) 2016 Efast Serv. All  Rights Reserved.
 * This software is published under the terms of the Efast Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2016-6-15 下午2:55:23        qubibo     1.0            Initial Version
 ****************************************************************************
 */
@Controller
@RequestMapping("/frame_center/base")
public class PortalBaseParameterController extends AbstractController{
	@Autowired
	private IPortalBaseParameterService portalBaseParameterService;
	@Autowired
	private IPortalCompanyUserService portalCompanyUserService;
	@Autowired
	private IBaseCompanyInfoService baseCompanyInfoService;
	@Autowired
	private IResourceService portalResourceService;
	
	


	
	/**
	 * list页面异步请求
	 * @Date        :      2016-6-7
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param searchValue
	 * @return
	 */
	
	@RequestMapping(value = "/parameter/list")
	public @ResponseBody ResultBean portalparameterList(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestBody PortalParameterQueryBean bean
			){
		Pagination<PortalBaseParameter> pager =  super.initPager(bean.getStart() / bean.getLength() + 1, bean.getLength(),
				bean.getOrderBy(), bean.getSort());
		pager = portalBaseParameterService.queryListBaseParameter(pager, bean);
		ResultBean result = new ResultBean(true,null);
		try {
			result.setObject(pager);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}

		return result;
	}
	
	
	
	
	
	
	/**
	 * 新增参数
	 * @Date        :      2016-6-7
	 * @param modelAndView
	 * @param moduleName
	 * @param paramKey
	 * @param i18nParamKey
	 * @param paramDesc
	 * @param i18nDesc
	 * @param paramScope
	 * @param paramLevel
	 * @param dataType
	 * @param paramValue
	 * @param valid
	 * @param modelMap
	 * @param request
	 * @return
	 *
*/
	@RequestMapping(value = "/parameter/insert",method = RequestMethod.POST)
	public @ResponseBody ResultBean rolePortalparameter(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestBody PortalBaseParameter portalBaseParameter
			){
		ResultBean result = new ResultBean(true,null);
		try {
			portalBaseParameterService.insert(portalBaseParameter);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
			/*portalBaseParameter.setId(portalBaseParameter.getEnterpriseId()+"."+portalBaseParameter.getUserId()+"."+portalBaseParameter.getModuleName()+"."+portalBaseParameter.getParamKey());
			portalBaseParameter.setI18nParamKey(portalBaseParameter.getModuleName()+"."+portalBaseParameter.getParamKey());
			portalBaseParameter.setI18nDesc(portalBaseParameter.getModuleName()+"."+portalBaseParameter.getParamKey());*/
			
	
		return result;
	}
	
	
	
	
	
	/**
	 * 修改参数
	 * @Date        :      2016-6-14
	 * @param id
	 * @param modelAndView
	 * @param baseParameter
	 * @param modelMap
	 * @param request
	 * @return
	 */
	
	
	@RequestMapping(value = "/parameter/edit",method = RequestMethod.POST)
	public @ResponseBody ResultBean parameterEdit(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestBody PortalBaseParameter portalBaseParameter
			){
		ResultBean result=new ResultBean(true,"");
		try{
			portalBaseParameterService.update(portalBaseParameter);
		}catch(Exception e){
			result.setSuccess(false);
			result.setErrorCode(e.getMessage());
			
		}
		return result;
	}
	
	
	/**
	 * 删除参数   把valid从0设为1
	 * @Date        :      2016-6-7
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/parameter/del")
	public @ResponseBody ResultBean delete(@RequestBody Map<String,Integer> map){
		
		ResultBean result=new ResultBean(true,null);
		Long id=null;
		if(map.containsKey("portalBaseParameterId")){
			id=Long.parseLong(map.get("portalBaseParameterId").toString());
		}
		try {
			portalBaseParameterService.del(id);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		return result;
	}
	
	/**
	 * 异步加载公司人员名单
	 * @Date        :      2016-6-14
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/parameter/user")
	public @ResponseBody
	List<PortalCompanyUser> findUserByCompanyCode(ModelMap modelMap ,@RequestParam String companyCode) {
		
		List<PortalCompanyUser> listpoPortalCompanyUsers = portalCompanyUserService.findUserByCompanyCode(companyCode);
		modelMap.addAttribute("listpoPortalCompanyUsers",listpoPortalCompanyUsers);
		
		return listpoPortalCompanyUsers;
	}
	
	
	

	
	
	/**
	 * list页面异步请求 我的参数管理
	 * @Date        :      2016-6-7
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param searchValue
	 * @return
	 */
	
	@RequestMapping(value = "/parameter/list_user")
	public @ResponseBody ResultBean portalparameterListUser(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestBody PortalParameterQueryBean bean){
		ResultBean result=new ResultBean(true,"");
		Pagination<PortalBaseParameter> pager =  super.initPager(bean.getStart() / bean.getLength() + 1, bean.getLength(),
				bean.getOrderBy(), bean.getSort());
		pager = portalBaseParameterService.pageMapParameterByUser(pager, bean.getModuleName());
		result.setObject(pager);
		return result;
	}
	
	
	
	
	
	
	/**
	 * 修改用户级别系统参数
	 * @Date        :      2016-6-14
	 * @param id
	 * @param modelAndView
	 * @param baseParameter
	 * @param modelMap
	 * @param request
	 * @return
	 */
	
	
	@RequestMapping(value = "/parameter/edit_parameter_user",method = RequestMethod.POST)
	public @ResponseBody ResultBean parameterEditUser(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestBody PortalBaseParameter portalBaseParameter
			){
		ResultBean result=new ResultBean(true,"");
		try{
			portalBaseParameterService.updateParameterByUser(portalBaseParameter);
		}catch(Exception e){
			result.setSuccess(false);
			result.setErrorCode(e.getMessage());
		}
		return result;
	}
	
	

	/**
	 * 修改企业级别系统参数
	 * @Date        :      2016-6-14
	 * @param id
	 * @param modelAndView
	 * @param baseParameter
	 * @param modelMap
	 * @param request
	 * @return
	 */
	
	
	@RequestMapping(value = "/parameter/edit_parameter_enterprise",method = RequestMethod.POST)
	public @ResponseBody ResultBean parameterEditByEnterprise(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestBody PortalBaseParameter portalBaseParameter
			){
		ResultBean result=new ResultBean(true,"");
		try{
			portalBaseParameterService.updateParameterByEnterprise(portalBaseParameter);
		}catch(Exception e){
			result.setSuccess(false);
			result.setErrorCode(e.getMessage());
		}
		return result;
	}
	
	
	
	
	
	/**
	 * list页面异步请求
	 * @Date        :      2016-6-7
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param searchValue
	 * @return
	 */
	
	@RequestMapping(value = "/parameter/list_enterprise")
	public @ResponseBody ResultBean portalparameterListEnterprise(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam(required = false) String moduleName,
			@RequestBody PortalParameterQueryBean bean){
		ResultBean result=new ResultBean(true,"");
		Pagination<PortalBaseParameter> pager =  super.initPager(bean.getStart() / bean.getLength() + 1, bean.getLength(),
				bean.getOrderBy(), bean.getSort());
		pager = portalBaseParameterService.pageMapParameterByEnterprise(pager, moduleName);
		result.setObject(pager);
		return result;
	}
	
	
	
	
	
	
	/**
	 * 查询企业拥有的模块
	 * @Date        :      2016-7-7
	 * @param portalBaseParameterId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/parameter/queryModules")
	public @ResponseBody List<ModuleInfo> queryModulesByCompanyCode(@RequestParam(required = false)Long portalBaseParameterId,
			HttpServletRequest request, 
			HttpServletResponse response,ModelMap model) {
		List<ModuleInfo> listModuleInfos = portalResourceService.queryModulesByCompanyCode();
		return listModuleInfos;
	}
	
	
	/**
	 * 通过paramKey 查询参数信息
	 * @param paramKey
	 * @return
	 * 
	 */
	@RequestMapping(value = "/parameter/paramKey")
	public @ResponseBody PortalBaseParameter queryByParamKey(String paramKey){
		
		return portalBaseParameterService.queryByParamKey(paramKey);
		
	}
}
